<?php
include "otamu_config.php";

define("_VIEWPATH_","views/sec_menu/");


//object 
		class Menu extends ADOdb_Active_Record
		{
			var $_table = 'menus';
		}

$act = $_REQUEST['act'];
		
switch ($act) {

	case "list":
		$menus = pg_query("SELECT * FROM menus ORDER BY sort ASC");
		break;

	case "new":
		$menu = new Menu();
		break;

	case "create":
		$menu = new Menu();

		$menu->caption = $_REQUEST['caption'];
		$menu->alt = $_REQUEST['alt'];
		$menu->url = $_REQUEST['url'];
		$menu->filename = $_REQUEST['filename'];
		$menu->filename2 = $_REQUEST['filename2'];
		$menu->is_parent = $_REQUEST['is_parent'];
		$menu->parent_id = $_REQUEST['parent'];
		$menu->sort = $_REQUEST['sort'];
		
		//print_r($menu);
		
		if ($menu->save()) {
			$_SESSION['flash'] = 'Menu berjaya disimpan';
			header("Location: ?act=list");
		} else {
			$_SESSION['flash'] = 'Error.';
			$act = 'new';
			//header("Location: ?act=user_list");			
		}
		break;

	case "edit":
		$id = $_REQUEST['id'];
		$menu = new Menu();
		$menu = $menu->Find("id=$id");
		if ($menu) {
			$menu_to_edit = $menu[0];
			print ($menu_to_edit->caption);
		} else {
			$_SESSION['flash'] = 'Menu tidak wujud';
			header("Location: ?act=list");
		}
		break;
	
	case "update":
		$id = $_REQUEST['id'];
		//print_r($_REQUEST);exit;
		$menu = new Menu();
		$menu = $menu->Find("id=$id");
		if ($menu) {
			$menu = $menu[0];
		} else {
			$_SESSION['flash'] = 'Menu tidak wujud';
			header("Location: ?act=list");
		}
		
		$menu->caption = $_REQUEST['caption'];
		$menu->alt = $_REQUEST['alt'];
		$menu->url = $_REQUEST['url'];
		$menu->filename = $_REQUEST['filename'];
		$menu->filename2 = $_REQUEST['filename2'];
		$menu->is_parent = $_REQUEST['is_parent'];
		$menu->parent_id = $_REQUEST['parent'];
		$menu->sort = $_REQUEST['sort'];
		
		if ($menu->save()) {
			$_SESSION['flash'] = 'Menu berjaya disimpan';
			header("Location: ?act=list");
		} else {
			$_SESSION['flash'] = 'Error';
			$act = 'new';
			//header("Location: ?act=user_list");			
		}
		break;
		
	case "destroy":
		$id = $_REQUEST['id'];
		pg_query("DELETE FROM menus WHERE id=$id");
		header("Location: ?act=list");
		break;

	case "edit_role_menu":
		$role_id = $_REQUEST['role_id'];
		$r = pg_query("SELECT * FROM roles where id=$role_id");	
		$the_role = pg_fetch_object($r,0);
		
		$menus = pg_query("SELECT * FROM menus ORDER BY sort");
		
		define("_VIEWPATH_",dirname(__FILE__)."/auth/views/");
		include _VIEWPATH_."otamu_layout.php";
		break;

	case "save_role_menu":
		$role_id = $_REQUEST['role_id'];
		$r = pg_query("SELECT * FROM roles where id=$role_id");	
		$the_role = pg_fetch_object($r,0);

		$joined_menus = "{" . join(",",$_REQUEST['menu_ids']) . "}";
		$sql = "INSERT INTO role_menus(role_name,menus) VALUES('$the_role->name','$joined_menus')";
		if (pg_query($sql)) {
			$_SESSION['flash'] = 'Menu untuk peranan $the_role->name berjaya disimpan';
			header("Location: pengguna.php?act=edit_role_menu");
		} else {
			$_SESSION['flash'] = 'Error.';
			$act = 'edit_role_menu';
		}
		break;

	default :
		$act = 'list';
		$menus = pg_query("SELECT * FROM menus ORDER BY sort");
		break;	
}

include _VIEWPATH_."otamu_layout.php";

?>
